/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is a free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gadglet.core;

import java.util.List;
import java.util.logging.Logger;

import com.gadglet.core.GadgletResponse.ContentRecord;
import com.gadglet.data.Friends;
import com.gadglet.data.FriendsInvitation;
import com.gadglet.data.FriendsInvitationUtils;
import com.gadglet.data.FriendsUtils;
import com.gadglet.data.UserProfile;
import com.gadglet.data.UserProfilesUtils;
import com.gadglet.params.ProfPrivacyStatus;
import com.gadglet.params.ProfileFields;
import com.gadglet.params.ReqErrorTypes;

/**
 * Basic implementation of methods support Minglets functionality.
 * Developer may Override these implementations
 *
 */
public abstract class MingletRequestHandler extends BasicRequestHandler {
	 Logger log = Logger.getLogger(this.getClass().getName());

	@Override
	public void doShare(GadgletRequestWrapper request, GadgletResponse res)
			throws RequestException {
		throw new RequestException(ReqErrorTypes.UNRECOGNIZED_ACTION);
	}

	@Override
	public void doSearch(GadgletRequestWrapper request, GadgletResponse res)
			throws RequestException {
		throw new RequestException(ReqErrorTypes.UNRECOGNIZED_ACTION);
	}

	
	/* 
	 * Save the current user profile
	 * @param request
	 * @param res
	 */
	@Override
	public void addProfile(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {

		try {
			// need to prevent the access from oauth requests
			UserProfilesUtils.createMyProfile(request);

		} catch (Exception e) {

			throw e;
		}
	}

	/* 
	 * 
	 * Save the current user profile
	 * @param request
	 * @param res
	 */
	@Override
	public void updateProfile(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {

		try {
			UserProfilesUtils.updateMyProfile(request);

		} catch (Exception e) {

			throw e;
		}
	}

	/* 
	 * 
	 * Get the current user profile
	 * @param request
	 * @param res
	 */
	@Override
	public void getProfile(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {

		UserProfile profile = null;
		try {

			profile = UserProfilesUtils.getMyProfile(request);

			ContentRecord profileRecord = res.newItem();

			profileRecord.addItemField(
					ProfileFields.PROFILE_NICKNAME.getParamName(),
					profile.getNickName());
			profileRecord.addItemField(
					ProfileFields.PROFILE_PRIVACY.getParamName(),
					profile.getVisibilityStatus().getPrivacyStatus());
			profileRecord.addItemField(
					ProfileFields.PROFILE_THUMBNAILURL.getParamName(),
					profile.getPhotoUrl());

		} catch (Exception e) {

			throw e;
		}
	}

	/* 
	 * 
	 * Create a new invitation for a user 
	 * @param request
	 * @param res
	 */
	@Override
	public void doInvite(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {
		
				FriendsInvitationUtils.createNewInvitation(
				request.getCurrentUserProfile(), request);
		
	
	}

	/* 
	 * 
	 * Accept invitation and create a "friend"
	 * @param request
	 * @param res
	 */
	@Override
	public void doAcceptFriend(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {
		//create friend & delete invitation 
		FriendsUtils.createNewFriends(request);


	}

	/* 
	 * Reject invitation 
	 * @param request
	 * @param res
	 */
	@Override
	public void doRejectFriend(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {

		FriendsInvitationUtils.deleteInvitation(request);

	}
	
	/* 
	 * Send to the client a list of the current user friends
	 * @param request
	 * @param res
	 */
	@Override
	public void doGetFriends(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {
		
		List <Friends> myFriends =  FriendsUtils.getMyFriends(request.getCurrentUserProfile());
		
		res.setHasMore(false);
		
		res.setAddPermit(false);
		
		if(myFriends==null)
			res.setTotalItemsNum(0);
		else{
			
			for(Friends friends: myFriends){
				
				ContentRecord record  = res.newItem();
				
				record.addItemKeyField(friends.getFriendId(request.getCurrentUserProfileId()));
				
				
				UserProfile profile = UserProfilesUtils.getProfileById(friends.getFriendId(request.getCurrentUserProfileId()));
				
				if(profile!=null && !profile.isPrivate()){
					record.addItemField(ProfileFields.PROFILE_NICKNAME.getParamName(), profile.getNickName());
					if( profile.getPhotoUrl()!=null &&  profile.getPhotoUrl().length()>0)
						record.addItemField(ProfileFields.PROFILE_THUMBNAILURL.getParamName(), profile.getPhotoUrl());
				}
							
				record.setContentName("myFriends");
				
				record.setGadgetPermissions(false, false, false);
			}
			
		}
		
	
	}
	
	/* 
	 * 
	 * Sent to the client a list of invitations 
	 * @param request
	 * @param res
	 */
	
	@Override
	public void doGetInvitations(GadgletRequestWrapper request,
			GadgletResponse res) throws Exception {
		int items = 0;
		
		List <FriendsInvitation> containerInvitations = FriendsInvitationUtils.getMyContainerInvitations(request);
		List <FriendsInvitation> platformInvitations = FriendsInvitationUtils.getMyPlatformInvitations(request);

		
		res.setHasMore(false);
		
		res.setAddPermit(false);
		
		if(containerInvitations==null && platformInvitations == null)
			res.setTotalItemsNum(0);
		else{
			if(containerInvitations!=null)
				items = containerInvitations.size();
			if(platformInvitations!=null)
				items = items + platformInvitations.size();
			
			
			res.setTotalItemsNum(items);
			
			for(FriendsInvitation invitation: containerInvitations){
				
				ContentRecord record  = res.newItem();
				
				record.addItemKeyField(invitation.getInvitationId());
				record.addItemField(ProfileFields.PROFILE_INVITED_BY.getParamName(), invitation.getInvitedBy());
				UserProfile profile = UserProfilesUtils.getProfileById(invitation.getInvitedBy());
				
				if(profile!=null && !profile.isPrivate()){
					record.addItemField(ProfileFields.PROFILE_NICKNAME.getParamName(), profile.getNickName());
					if( profile.getPhotoUrl()!=null &&  profile.getPhotoUrl().length()>0)
						record.addItemField(ProfileFields.PROFILE_THUMBNAILURL.getParamName(), profile.getPhotoUrl());

				}
				else
					record.addItemField(ProfileFields.PROFILE_NICKNAME.getParamName(),ProfPrivacyStatus.PRIVATE.getPrivacyStatus());
				
				record.setContentName("myPendingInvitatos");
				
				record.setGadgetPermissions(false, false, false);
			}
			
			for(FriendsInvitation invitation: platformInvitations){
				
				ContentRecord record  = res.newItem();
				
				record.addItemKeyField(invitation.getInvitationId());
				record.addItemField(ProfileFields.PROFILE_INVITED_BY.getParamName(), invitation.getInvitedBy());
				UserProfile profile = UserProfilesUtils.getProfileById(invitation.getInvitedBy());
				
				if(profile!=null && !profile.isPrivate()){
					record.addItemField(ProfileFields.PROFILE_NICKNAME.getParamName(), profile.getNickName());
					if( profile.getPhotoUrl()!=null &&  profile.getPhotoUrl().length()>0)
						record.addItemField(ProfileFields.PROFILE_THUMBNAILURL.getParamName(), profile.getPhotoUrl());
				}
				else
					record.addItemField(ProfileFields.PROFILE_NICKNAME.getParamName(),ProfPrivacyStatus.PRIVATE.getPrivacyStatus());
								
				record.setContentName("myPendingInvitatos");
				
				record.setGadgetPermissions(false, false, false);
			}
		}
	
	}

}
